<template>
  <div class="registration-container">
    <div class="registration-header">
      <h2>报名管理</h2>
      <el-button type="primary" @click="handleAdd">报名</el-button>
    </div>

    <!-- 报名列表 -->
    <el-table :data="registrationList" v-loading="loading" border>
      <el-table-column prop="eventName" label="赛事名称" />
      <el-table-column prop="name" label="姓名" />
      <el-table-column prop="age" label="年龄" width="80" />
      <el-table-column prop="gender" label="性别" width="80">
        <template #default="{ row }">
          {{ row.gender === 'male' ? '男' : '女' }}
        </template>
      </el-table-column>
      <el-table-column prop="group" label="分组" width="100">
        <template #default="{ row }">
          {{ row.gender === 'male' ? '男子组' : '女子组' }}
        </template>
      </el-table-column>
      <el-table-column prop="registrationTime" label="报名时间" width="180" />
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 报名对话框 -->
    <el-dialog
      v-model="dialogVisible"
      title="赛事报名"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="赛事项目" prop="eventId">
          <el-select 
            v-model="form.eventId" 
            placeholder="请选择赛事项目"
            style="width: 100%"
          >
            <el-option 
              v-for="item in eventOptions" 
              :key="item.id" 
              :label="item.name" 
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number 
            v-model="form.age" 
            :min="1"
            :max="100"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="form.gender">
            <el-radio label="male">男</el-radio>
            <el-radio label="female">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit" :loading="submitting">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      v-model="detailVisible"
      title="报名详情"
      width="500px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="赛事名称">
          {{ currentDetail?.eventName }}
        </el-descriptions-item>
        <el-descriptions-item label="姓名">
          {{ currentDetail?.name }}
        </el-descriptions-item>
        <el-descriptions-item label="年龄">
          {{ currentDetail?.age }}
        </el-descriptions-item>
        <el-descriptions-item label="性别">
          {{ currentDetail?.gender === 'male' ? '男' : '女' }}
        </el-descriptions-item>
        <el-descriptions-item label="分组">
          {{ currentDetail?.gender === 'male' ? '男子组' : '女子组' }}
        </el-descriptions-item>
        <el-descriptions-item label="报名时间">
          {{ currentDetail?.registrationTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'

const userStore = useUserStore()

// 搜索相关
const searchForm = reactive({
  eventName: ''
})

const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

const resetSearch = () => {
  searchForm.eventName = ''
  handleSearch()
}

// 列表相关
const loading = ref(false)
const registrationList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

const fetchData = async () => {
  loading.value = true
  try {
    await new Promise(resolve => setTimeout(resolve, 500))
    registrationList.value = [
      {
        id: 1,
        eventName: '100米跑',
        name: '张三',
        age: 20,
        gender: 'male',
        registrationTime: '2024-03-11 10:00:00'
      }
    ]
    total.value = 1
  } catch (error) {
    ElMessage.error('获取数据失败')
  } finally {
    loading.value = false
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

// 报名相关
const dialogVisible = ref(false)
const submitting = ref(false)
const formRef = ref(null)

// 赛事选项
const eventOptions = ref([
  { id: 1, name: '100米跑' },
  { id: 2, name: '200米跑' },
  { id: 3, name: '跳高' },
  { id: 4, name: '跳远' }
])

const form = reactive({
  eventId: '',
  name: '',
  age: 18,
  gender: 'male'
})

const rules = {
  eventId: [
    { required: true, message: '请选择赛事项目', trigger: 'change' }
  ],
  name: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  age: [
    { required: true, message: '请输入年龄', trigger: 'blur' }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ]
}

const handleAdd = () => {
  form.eventId = ''
  form.name = ''
  form.age = 18
  form.gender = 'male'
  dialogVisible.value = true
}

const handleSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      submitting.value = true
      try {
        await new Promise(resolve => setTimeout(resolve, 1000))
        ElMessage.success('报名成功')
        dialogVisible.value = false
        fetchData()
      } catch (error) {
        ElMessage.error('报名失败')
      } finally {
        submitting.value = false
      }
    }
  })
}

// 查看详情相关
const detailVisible = ref(false)
const currentDetail = ref(null)

const handleEdit = (row) => {
  currentDetail.value = row
  detailVisible.value = true
}

// 格式化日期
const formatDate = (date) => {
  if (!date) return ''
  return date
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.registration-container {
  padding: 20px;
}

.registration-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-form {
  margin-bottom: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 4px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

:deep(.el-descriptions__label) {
  width: 120px;
}
</style> 